package com.dmzc.stage2.chapter27;

import java.util.LinkedList;

public class ActiveMessageQueue {
    private final LinkedList<MethodMessage> messages = new LinkedList<>();
    public ActiveMessageQueue(){
        new ActiveDaemonThread(this).start();
    }
    public void offer(MethodMessage methodMessage){
        synchronized (this){
            messages.addLast(methodMessage);
            this.notify();
        }
    }
    protected MethodMessage take(){
        synchronized (this){
            while (messages.isEmpty()){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return messages.removeFirst();
        }
    }


}
